package com.smqj.inspect;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;

public class InspectInfoExcelListener extends AnalysisEventListener<InspectInfo> {

    private static Logger logger = LogManager.getLogger(InspectInfoExcelListener.class.getName());
    /**
     * 批处理阈值
     */
    private static final int BATCH_COUNT = 2;
    List<InspectInfo> list = new ArrayList<InspectInfo>(BATCH_COUNT);

    private static final String[] MORE_RANGE = ResourceBundle.getBundle("inspect").getString("test.item.more.range").split(",");
    private static final String[] RESULT_ONLY = ResourceBundle.getBundle("inspect").getString("test.item.result.only").split(",");
    private static final String[] NO_UNIT = ResourceBundle.getBundle("inspect").getString("test.item.no.unit").split(",");

    private boolean isTestItemTrue(String info) {
        String errorStart = ResourceBundle.getBundle("inspect").getString("test.item.start.error");
        String[] errorStartArray = errorStart.split(",");
        for (String error : errorStartArray) {
            if (info.indexOf(error) > -1) {
                return false;
            }
        }
        return true;
    }

    @Override
    public void invoke(InspectInfo inspectInfo, AnalysisContext analysisContext) {
//        logger.info("解析到一条数据:"+inspectInfo.toString());
        String[] testItemList = inspectInfo.toString().split("\\n");
        testItemList = Arrays.copyOfRange(testItemList, 3, testItemList.length - 3);
        String tempValue = "";
        for (String testItem : testItemList) {
            if (testItem.length() > 0 && isTestItemTrue(testItem)) {
                testItem = testItem.replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ").replaceAll("  ", " ");
                try {
                    String resultItem = "";
                    String[] result = testItem.trim().split(" ");
                    if (result.length > 2 && (result[1].equals("H") || result[1].equals("L"))) {
                        testItem = result[0] + " " + result[1] + result[2];
                        for (int i = 3; i < result.length; i++) {
                            testItem += " " + result[i];
                        }
                        result = testItem.trim().split(" ");
                    }
                    if (result.length > 1 && result[0].equals(".(") && result[1].endsWith(")")) {
                        testItem = result[0] + result[1];
                        for (int i = 2; i < result.length; i++) {
                            testItem += " " + result[i];
                        }
                        result = testItem.trim().split(" ");
                    }
                    for (String temp : RESULT_ONLY) {
                        tempValue = temp;
                        if (testItem.indexOf(temp) > -1) {
                            if (result.length == 1) {
                                resultItem = "检查项目：" + result[0].split("：")[0] + "：，检查结果:" + result[0].split("：")[1];
                            } else {
                                resultItem = "检查项目：" + result[0] + "，检查结果:" + result[1];
                            }
                            break;
                        }
                    }
                    if (resultItem.length() == 0) {
                        for (String temp : NO_UNIT) {
                            tempValue = temp;
                            if (testItem.indexOf(temp) > -1) {
                                resultItem = "检查项目：" + result[0] + "，检查结果:" + result[1] + "，检查范围:" + result[2];
                                break;
                            }
                        }
                    }
                    if (resultItem.length() == 0) {
                        for (String temp : MORE_RANGE) {
                            tempValue = temp;
                            if (testItem.indexOf(temp) > -1) {
                                if (result[2].length() > 6) {
                                    resultItem = "检查项目：" + result[0] + "，检查结果:" + result[1] + "，检查单位:" + result[2].substring(0, 6) + "，检查范围:" + result[2].substring(6);
                                } else {
                                    resultItem = "检查项目：" + result[0] + "，检查结果:" + result[1] + "，检查单位:" + result[2] + "，检查范围:";
                                }
                                if (result.length > 3) {
                                    for (int j = 3; j < result.length; j++) {
                                        resultItem += " " + result[j];
                                    }
                                }
                                break;
                            }
                        }
                    }
                    if (resultItem.length() == 0) {
                        if (result.length >= 4) {
                            int start = 3;
                            if (result[2].length() > 6) {
                                resultItem = "检查项目：" + result[0] + "，检查结果:" + result[1] + "，检查单位:" + result[2].substring(0, 6) + "，检查范围:" + result[2].substring(6);
                            } else {
                                resultItem = "检查项目：" + result[0] + "，检查结果:" + result[1] + "，检查单位:" + result[2] + "，检查范围:";
                            }
                            if (result.length > start) {
                                for (int j = start; j < result.length; j++) {
                                    resultItem += " " + result[j];
                                }
                            }
                        } else {
                            resultItem = "非正常检查项，应该是上面的检查项遗漏的";
                        }

                    }
                    System.out.println(resultItem);
                } catch (ArrayIndexOutOfBoundsException e) {
                    e.printStackTrace();
                }
                System.out.println(testItem);
                System.out.println("**************************************************");
            }
        }
        list.add(inspectInfo);
        if (list.size() >= BATCH_COUNT) {
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//        saveData();
//        logger.info("所有数据解析完成！");
    }

    private void saveData() {
//        logger.info( list.size()+"条数据，开始存储数据库！");
//        logger.info("存储数据库成功！");
    }

}
